package main_package;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

/**
 * this is the main class
 */
public class Main 
{

	/**
	 * this is the mmain function of this program
	 * @param args
	 */
	public static void main(String[] args)
	{
		//initializing variables
		String action_case, str, file_name;
		int key, rows, cols;
		FileReader fr = null;
		BufferedReader br = null;
		ArrayList<ArrayList<Integer>> row_cons;
		ArrayList<ArrayList<Integer>> col_cons;
		GA_griddlers_alg gga = new GA_griddlers_alg();
		
		
		Scanner in = new Scanner(System.in);
		System.out.println("please enter the action case number: 1 - for the GA Griddlers algorithm, 2 - for the darwin GA Griddlers algorithm, 3 - for the lamark GA Griddlers algorithm");
		action_case = in.nextLine();

		try 
		{
			// convert the case number string into an integer
			key = Integer.parseInt(action_case);
			
			System.out.println("please enter the input file name");
			file_name = in.nextLine();
			fr = new FileReader(file_name);
			br = new BufferedReader(fr);
			
			// read the input
			str = br.readLine();
			rows = Integer.parseInt(str);
			str = br.readLine();
			cols = Integer.parseInt(str);
			str = br.readLine();
			row_cons = create_constrains(str, rows);
			str = br.readLine();
			col_cons = create_constrains(str, cols); 
	
			fr.close();
			br.close();
			
			// printing the date of beginning of the algorithm
			System.out.println(new Date());
			
			// go to the correct section (according to the user input)
			switch (key) 
			{
			case 1:
				// run the normal GA algorithm
				gga.run_alg(row_cons, col_cons);
				break;
				
			case 2:
				// run the darwin GA algorithm
				gga.run_darwin_alg(row_cons, col_cons);
				break;
				
			case 3:
				// run the lamark GA algorithm
				gga.run_lamark_alg(row_cons, col_cons);
				break;

			default:
				System.out.println("case doesn't exist - try again");
				break;
			}
			
			//printing date of finish
			System.out.println(new Date());
			
		}
		catch (Exception e)
		{
			System.out.println("problem");
		}
	}

	/*
	 * this function create the constrains array of the given str
	 * @param str - the constrains
	 * @param size - number of constrains
	 * @return - the constrains array
	 */
	private static ArrayList<ArrayList<Integer>> create_constrains(String str,
			int size) {
		ArrayList<ArrayList<Integer>> cons = new ArrayList<ArrayList<Integer>>(size);
		ArrayList<Integer> curr_cons;
		String[] temp = str.split(":");
		String temp2;
		String[] temp3;
		
		//for every constrain
		for (int i = 0; i < temp.length; i++) 
		{
			curr_cons = new ArrayList<Integer>();
			temp2 = temp[i];
			temp3 = temp2.split("[,()]+");
			
			// for every number of black cells
			for (int j = 0; j < temp3.length; j++) 
			{
				if(!temp3[j].isEmpty())
				{
					// insert into the constrains array
					curr_cons.add(Integer.parseInt(temp3[j]));
				}
			}
			
			cons.add(curr_cons);
		}
		
		return cons;
	}
}
